<template>
  <el-dialog
    :title="!dataForm.jobId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="dataForm.taskName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="任务描述" prop="taskDescription">
        <el-input v-model="dataForm.taskDescription" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="是否默认表" prop="isMrTable">
        <el-radio @change="changeradio('1')" v-model="radio" label="1">否</el-radio>
        <el-radio @change="changeradio('2')" v-model="radio" label="2">是</el-radio>
      </el-form-item>
      <el-form-item v-if="radio=='1'" label="接口名称" prop="interfaceName">
        <el-select v-model="dataForm.interfaceName" placeholder="请选择">
          <el-option
            v-for="(item,index) in interfaceNameoptions"
            :key="index"
            :label="item.name"
            :value="item.interfaceName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="radio=='2'" label="表名称" prop="tableName">
        <el-select v-model="dataForm.tableName" @change="getOtherVal(dataForm.tableName)" placeholder="请选择">
          <el-option
            v-for="(item,index) in options"
            :key="index"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="方法名称" prop="methodName">
        <el-input v-model="dataForm.methodName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="bean名称" prop="beanName">
        <el-input v-model="dataForm.beanName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="参数" prop="params">
        <el-input @focus="editJson" v-model="dataForm.params" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="cron表达式" prop="cronExpression">
        <el-input v-model="dataForm.cronExpression" placeholder=""></el-input>
        <p style="padding:0;margin:0">
          参考地址：<a href="https://qqe2.com/cron" target="_blank" rel="noopener noreferrer"> https://qqe2.com/cron</a>
        </p>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>


    <el-dialog
      width="30%"
      title="JSON参数编辑"
      :visible.sync="editPostJsonFlag"
      append-to-body>
      <div v-if="editPostJsonFlag">
        <vue-json-editor
          v-model="resultInfo"
          :showBtns="false"
          :mode="'code'"
          @json-change="onJsonChange"
          @json-save="onJsonSave"
          @has-error="onError"
        />
        <br>
      <el-button type="primary" @click="checkJson">确定</el-button>
    </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
 import vueJsonEditor from 'vue-json-editor'
 import request from '@/utils/request'
  export default {
    data () {
      return {
        resultInfo:{},
        editPostJsonFlag:false,
        radio:'1',
        visible: false,
        options:[],
        isMrTable:false,
        opt:null,
        interfaceNameoptions:[],
        dataForm: {
          jobId:null,
          interfaceName:"",
          beanName: 'TimingApiTask',  
          params: '',
          cronExpression: '',
          remark: '',
          taskName:"",
          taskDescription:"",
          tableName:"",
          methodName:"runTask",
          status: 0
        },
        dataRule: {
          beanName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          // cronExpression: [
          //   { required: true, message: 'cron表达式不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    components:{
      vueJsonEditor
    },
    mounted(){
      this.$nextTick(()=>{
        this.getSelectDataList()
        this.getInterfaceList()
      })
    },
    methods: {
      changeradio(v){
         this.$set(this.dataForm,'interfaceName','')  
         this.$set(this.dataForm,'params','')  
         this.$set(this.dataForm,'tableName','')  
         v=='2'?this.$set(this.dataForm,'methodName','runTableTask'):this.$set(this.dataForm,'methodName','runTask')
         this.resultInfo = {}
         this.$forceUpdate()
      },
        // 获取数据列表
      getInterfaceList (v) {
        request({
          url: '/ht-datadevelop/api/real/manage/list',
          method: 'post',
          data:{limit:"1000",name:"",page:"1"}
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.interfaceNameoptions = data.page.list
          } else {
            this.interfaceNameoptions = []
          }
        })
      },
      getOtherVal(v){
        this.options.forEach((item,index)=>{
          if(item.name==v){
            this.opt = JSON.parse(JSON.stringify(item))
          }
        })
        delete this.opt.name
        this.resultInfo = JSON.parse(JSON.stringify(this.opt))
        this.dataForm.params = JSON.stringify(this.opt)
      },
        // input框获得焦点
      editJson (v) {
        if(this.dataForm.params&&JSON.parse(this.dataForm.params)){
          this.resultInfo = JSON.parse(this.dataForm.params)
        }
        this.editPostJsonFlag = true
      },
      onJsonChange (value) {
        // 实时保存
        this.onJsonSave(value)
      },
      onError(value) {
        this.hasJsonFlag = false
      },
      //确定按钮 检查json
      checkJson(){
        this.dataForm['params'] = JSON.stringify(this.resultInfo)
        this.editPostJsonFlag = false
        return true
      },
      // 输入值的时候调用
      onJsonSave (value) {
        this.resultInfo = value
        this.hasJsonFlag = true
      },
      reset(){
        this.dataForm = {
          jobId:null,
          interfaceName:"",
          beanName: 'TimingApiTask',
          params: '',
          cronExpression: '',
          remark: '',
          taskName:"",
          taskDescription:"",
          tableName:"",
          methodName:"",
          status: 0
        }
      },
      close(){
        this.visible = false
      },
      getSelectDataList(){
        request({
          url: '/ht-datadevelop/timing/api/getTableNameList',
          method: 'get',
          params: {}
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.options = data.data
          }
        })
      },
      init (row) {
        this.visible = true
        this.resultInfo = {}
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm = row
          console.log(row)
          if(!row.interfaceName){
            this.radio = '2'
          }else{
            this.radio = '1'
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            let url = ''
            let data = {}

            if(!this.dataForm.jobId){
              url = '/ht-datadevelop/timing/api/save'
              data = {
                "taskName":this.dataForm.taskName,
                "beanName":this.dataForm.beanName,
                "interfaceName": this.radio=='2'?null:this.dataForm.interfaceName||null,
                "methodName":this.dataForm.methodName,
                "jsonParams":this.dataForm.params?JSON.parse(this.dataForm.params):null,
                "cronExpression": this.dataForm.cronExpression,
                "remark":this.dataForm.remark
              }
            }else{
              url = '/ht-datadevelop/timing/api/update'
              data = {
                  "jobId":this.dataForm.jobId,
                  "taskName":this.dataForm.taskName,
                  "beanName":this.dataForm.beanName,
                  "interfaceName":this.radio=='2'?null:this.dataForm.interfaceName||null,
                  "methodName":this.dataForm.methodName,
                  "status":this.dataForm.status,
                  "jsonParams":this.dataForm.params?JSON.parse(this.dataForm.params):null,
                  "cronExpression": this.dataForm.cronExpression,
                  "remark":this.dataForm.remark
                }
            }

            console.log(data)
            request({
              url:url,
              method: 'post',
              data:data
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>



<style lang="scss" scoped>
// /deep/.el-select{
//   width:100% !important;
// }

  .el-form{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    .el-form-item{
      width:50%;
    }
  }</style>